<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/13
 * Time: 13:54
 */

namespace App\HttpController\Home;


use App\Utility\Pool\MysqlObject;
use App\Utility\Pool\MysqlPool;

class Hero extends Base
{
    /**
     * 英雄列表
     * @return bool
     * @throws \EasySwoole\Component\Pool\Exception\PoolEmpty
     * @throws \EasySwoole\Component\Pool\Exception\PoolException
     * @throws \Throwable
     */
    public function lists()
    {
        $params = $this->request()->getQueryParams();
        if( !isset($params['game']) || !$params['game'] ){
            return $this->writeJson(0,null,'缺少game参数');
        }

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 20;
        $table = "siam_{$params['game']}heroes";
        $lists = MysqlPool::invoke(function (MysqlObject $db)use($table,$page,$pageSize){
            return $db->get($table,[$pageSize * ($page  - 1), $pageSize]);
        });
        return $this->writeJson(1,$lists,'请求成功');
    }

    public function info()
    {
        $params = $this->request()->getQueryParams();
        if( !isset($params['game']) || !$params['game'] ){
            return $this->writeJson(0,null,'缺少game参数');
        }
        if( !isset($params['hero_id']) || !$params['hero_id'] ){
            return $this->writeJson(0,null,'缺少hero_id参数');
        }
        $table = "siam_{$params['game']}heroes";

        $info = MysqlPool::invoke(function (MysqlObject $db)use($table,$params){
            return $db->where('hero_id',$params['hero_id'])->getOne($table);
        });
        return $this->writeJson(1,$info,'请求成功');
    }
}